package com.qf.mpswaggershiro.product.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mpswaggershiro.product.entity.Product;
import com.qf.mpswaggershiro.product.entity.ProductImg;
import com.qf.mpswaggershiro.product.entity.ProductSku;
import com.qf.mpswaggershiro.product.mapper.ProductMapper;
import com.qf.mpswaggershiro.product.service.IProductImgService;
import com.qf.mpswaggershiro.product.service.IProductService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.qf.mpswaggershiro.product.service.IProductSkuService;
import com.qf.mpswaggershiro.vo.ProductInfoVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import java.util.stream.Collectors;

/**
 * <p>
 * 商品 商品信息相关表：分类表，商品图片表，商品规格表，商品参数表 服务实现类
 * </p>
 *
 * @author cui
 * @since 2023-02-13
 */
@Service
public class ProductServiceImpl extends ServiceImpl<ProductMapper, Product> implements IProductService {
       @Autowired
        ProductMapper productDao;
        @Autowired
       IProductImgService productImgService;
        @Autowired
        IProductSkuService productSkuService;

    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public List<Product> listProduct() {
        //分页查询
        Page<Product> productPage = new Page<>(1,3);

        QueryWrapper<Product> productQueryWrapper = new QueryWrapper<>();
        //按照创建时间倒序排序
        productQueryWrapper.orderByDesc("create_time");
        //执行分页查询 第二个参数是条件
        Page<Product> page = productDao.selectPage(productPage, productQueryWrapper);

       //执行完分页方法后，获取分页对象具有所以分页相关信息（当前页的数据，总条数）
        List<Product> records = page.getRecords();

        //查询productImg中有product的id的
        QueryWrapper<ProductImg> productImgQueryWrapper = new QueryWrapper<>();
        //固定写法.stream().map   把list<object>---->list<string>
        List<String> idList = records.stream().map(Product::getProductId).collect(Collectors.toList());
        //找到item_id中和分页查询表中相同的id
        productImgQueryWrapper.in("item_id",idList);
      //拿到需要的list数据
        List<ProductImg> list = productImgService.list(productImgQueryWrapper);

        //将原本list中的id和img数据  放入map中  变成key-->id  value-->img
        Map<String, List<ProductImg>> listMap = list.stream().collect(Collectors.groupingBy(ProductImg::getItemId));

        records.forEach(new Consumer<Product>() {
            @Override
            public void accept(Product product) {
                List<ProductImg> productImgs = listMap.get(product.getProductId());
                if (productImgs == null) {
                    productImgs =new ArrayList<>();
                }
                product.setImgs(productImgs);

            }
        });


        return records;
    }

    /**
     * 商品详情页
     * @param id
     * @return
     */
    @Transactional(propagation = Propagation.SUPPORTS)
    @Override
    public ProductInfoVo listproductInfo(Integer id) {

        QueryWrapper<Product> product_id = new QueryWrapper<Product>().eq("product_id", id);
        Product one = getOne(product_id);

        List<ProductImg> item_id = productImgService.list(new QueryWrapper<ProductImg>().eq("item_id", id));
        one.setImgs(item_id);

        List<ProductSku> productskus_id1 = productSkuService.list(new QueryWrapper<ProductSku>().eq("product_id", id));

        ProductInfoVo productInfoVo = new ProductInfoVo();
        productInfoVo.setProduct(one);
        productInfoVo.setProductImgs(item_id);
        productInfoVo.setProductSkus(productskus_id1);


        return productInfoVo;
    }

}
